<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="linkName">
              <el-input v-model="filters.linkName" clearable placeholder="联系人"></el-input>
            </el-form-item>
            <el-form-item prop="linkPhone">
              <el-input v-model="filters.linkPhone" clearable placeholder="联系电话"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="住户类型">
          <template slot-scope="scope">
            <div>
              {{ formatHouseType(scope.row.houseType) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="联系人" prop="linkName"> </el-table-column>
        <el-table-column align="center" label="联系电话" prop="linkPhone"> </el-table-column>
        <!-- <el-table-column align="center" label="省份" prop="provinceName">

        </el-table-column>
        <el-table-column align="center" label="城市" prop="cityName">

        </el-table-column>
        <el-table-column align="center" label="区名称" prop="areaName">

        </el-table-column> -->
        <el-table-column align="center" label="联系地址">
          <template slot-scope="scope">
            <div>
              {{ linkAddress(scope.$index) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <!-- <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{scope.row.state == 0? '启用' : '禁用'}}</el-dropdown-item> -->
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import EquipApi from '@/api/equip/equip';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';

export default {
  name: 'SpecimenManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    let _this = this;
    return {
      dataList: [],
      listLoading: true,
      filters: {
        linkName: '',
        linkPhone: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_SEARCH',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_DELETE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  watch: {
    //to去哪，from从哪来
    // $route(to,from){
    //   if(from.path =='/equip/specimenEdit' || from.path=='/equip/specimenAdd'){
    //       this.listData();
    //   }
    // }
  },
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    // 把省市区和详细地址合并起来为联系地址
    linkAddress(index) {
      return this.dataList[index].provinceName + this.dataList[index].cityName + this.dataList[index].areaName + this.dataList[index].address;
    },
    formatState(cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue == -1) {
        return 'danger';
      } else if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      }
    },
    formatHouseType(cellValue) {
      if (cellValue == 1) {
        return '住家';
      } else if (cellValue == 2) {
        return '办公';
      }
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/equip/householdAdd');
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/householdEdit',
        query: { id: row.houseId },
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/householdDetail',
        query: { id: row.houseId },
      });
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state == 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        EquipApi.deleteBeeperHouse({
          id: row.houseId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        EquipApi.deleteBeeperHouse({
          id: val.houseId,
          state: -1,
        }).then((res) => {
          this.$Utils.notifySuccess();
          _this.listData();
        });
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
      };
      cond.linkName = _this.filters.linkName;
      cond.linkPhone = _this.filters.linkPhone;
      // if (linkName !== undefined && linkName !== null && linkName !== '') {
      //   cond.linkName = linkName
      // }
      // if (linkPhone !== undefined && linkPhone !== null && linkPhone !== '') {
      //   cond.linkPhone = linkPhone
      // }
      EquipApi.listBeeperHouse(cond)
        .then((res) => {
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
          _this.listLoading = false;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
